using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace BibleScreen
{
    internal static class Program
    {
        public static void Main(string[] args)
        {
            // Set culture
            Thread.CurrentThread.CurrentCulture = Options.Culture;
            Thread.CurrentThread.CurrentUICulture = Options.Culture;

            // Check if we have got any args to act upon
            if (args.Length > 0)
            {
                switch (args[0].Trim().Substring(0, 2))
                {
                        // Display options dialog
                    case "/c":
                    case "/C":
                        var f = new OptionsForm();
                        Application.Run(f);
                        return;

                        // Display preview
                    case "/p":
                    case "/P":
                        // No preview
                        return;

                        // Show screensaver
                    case "/s":
                    case "/S":
                        // Run as usual
                        break;
                }
            }

            // Handle unhandled exceptions
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            Application.ThreadException += Application_ThreadException;

            // Start screen saver
            var s = new ScreenSaverForm();
            Application.Run(s);
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            LogException(e.Exception);
            Application.Exit();
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogException((Exception) e.ExceptionObject);
        }

        private static void LogException(Exception e)
        {
            Console.WriteLine(e.Message);

            FileInfo file = Options.LogFile;

            string message = string.Format(Text.CrashMessage, e.Message, Environment.NewLine, DateTime.Now);

            using (StreamWriter s = file.CreateText())
                s.Write(message);

            Process.Start(file.FullName);
        }

        //TODO: mulighet for statistikk utprintning
        //private static void PrintUsage()
        //{

        //    Thread t = new Thread(new ThreadStart(PrintUsage));
        //    t.IsBackground = true;
        //    t.Start();
        //    long mem = 0;

        //    Process p = Process.GetProcessesByName("BibleScreen")[0];
        //    while (true)
        //    {
        //        p.Refresh();
        //        if (p.PrivateMemorySize64 > mem)
        //        {
        //            mem = p.PrivateMemorySize64;
        //            Console.ForegroundColor = ConsoleColor.White;
        //        }
        //        else
        //        {
        //            Console.ForegroundColor = ConsoleColor.Gray;
        //        }
        //        Console.WriteLine("PrivMem: {0} MB\tHandles:{2}\tThreads: {1}", p.PrivateMemorySize64/1024/1024, p.Threads.Count, p.HandleCount);
        //        Thread.Sleep(500);
        //    }
        //}
    }
}